package cn.stylefeng.guns.modular.account.controller;

import cn.stylefeng.guns.core.common.constant.factory.PageFactory;
import cn.stylefeng.guns.core.common.page.PageInfoBT;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.modular.classification.service.IClassificationService;
import cn.stylefeng.guns.modular.family.service.IFamilyService;
import cn.stylefeng.guns.modular.familynum.service.IFamilynumService;
import cn.stylefeng.guns.modular.system.model.*;
import cn.stylefeng.guns.modular.system.service.IUserService;
import cn.stylefeng.guns.modular.system.warpper.AccountWarpper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.account.service.IAccountService;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 账户表控制器
 *
 * @author fengshuonan
 * @Date 2021-04-10 19:39:30
 */
@Controller
@RequestMapping("/account")
public class AccountController extends BaseController {

    private String PREFIX = "/account/account/";

    private String guanliyuan="1";

    private String huzhu="5";

    private String jiatingchengyuan="6";

    private String yonghu="7";

    @Autowired
    private IAccountService accountService;

    @Autowired
    private IClassificationService classificationService;

    @Autowired
    private IFamilyService familyService;

    @Autowired
    private IFamilynumService familynumService;

    @Autowired
    private IUserService userService;

    /**
     * 跳转到账户表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        EntityWrapper<Classification> classificationEntityWrapper=new EntityWrapper<>();
        classificationEntityWrapper.eq("pcode","zhlx");
        List<Classification> classificationList=classificationService.selectList(classificationEntityWrapper);
        model.addAttribute("classificationList",classificationList);

        return PREFIX + "account.html";
    }

    /**
     * 跳转到添加账户表
     */
    @RequestMapping("/account_add")
    public String accountAdd(Model model) {
        EntityWrapper<Classification> wrapper=new EntityWrapper<>();
        wrapper.eq("pcode","zhlx"); //账户类型

        //账户类型
        List<Classification> classifications=classificationService.selectList(wrapper);
        model.addAttribute("classifications",classifications);

        //用户id
        Integer userid=ShiroKit.getUser().getId();
        model.addAttribute("userid",userid);


        return PREFIX + "account_add.html";
    }

    /**
     * 跳转到修改账户表
     */
    @RequestMapping("/account_update/{accountId}")
    public String accountUpdate(@PathVariable Integer accountId, Model model) {

        EntityWrapper<Classification> wrapper=new EntityWrapper<>();
        wrapper.eq("pcode","zhlx");
        List<Classification> classifications=classificationService.selectList(wrapper);
        model.addAttribute("classifications",classifications);

        Account account = accountService.selectById(accountId);
        model.addAttribute("item",account);
        LogObjectHolder.me().set(account);
        return PREFIX + "account_edit.html";
    }

    /**
     * 获取账户表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,String condition2) {
//        return accountService.selectList(null);

        Page<Map<String,Object>> page=new PageFactory<Map<String,Object>>().defaultPage();
        EntityWrapper<Account> accountEntityWrapper=new EntityWrapper<>();

        accountEntityWrapper.orderBy("id",false);

        Integer userid=ShiroKit.getUser().getId(); //用户id
        String roleid=userService.selectById(userid).getRoleid();//当前用户的角色id
        accountEntityWrapper.eq("userid",userid);

        if(condition2!=null){
            System.out.println("*******"+condition2);
            accountEntityWrapper.like("accountkindid",condition2);
            accountEntityWrapper.like("accountname",condition);
        }
        else{
            accountEntityWrapper.like("accountname",condition);
        }


        Page<Map<String,Object>> mapPage=accountService.selectMapsPage(page,accountEntityWrapper);
        page=new AccountWarpper(mapPage).wrap();
        return new PageInfoBT<>(page);
    }

    /**
     * 新增账户表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Account account) {
        account.setCreatetime(new Date());
        accountService.insert(account);
        return SUCCESS_TIP;
    }

    /**
     * 删除账户表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer accountId) {
        accountService.deleteById(accountId);
        return SUCCESS_TIP;
    }

    /**
     * 修改账户表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Account account) {
        account.setUpdatetime(new Date());
        accountService.updateById(account);
        return SUCCESS_TIP;
    }

    /**
     * 账户表详情
     */
    @RequestMapping(value = "/detail/{accountId}")
    @ResponseBody
    public Object detail(@PathVariable("accountId") Integer accountId) {

        return accountService.selectById(accountId);
    }
}
